package com.omenkk.juc.ReentrantLock;
//reentrantLock的可打断性,获得锁失败进入阻塞状态，这个状态可以被打断,是解决死锁的一种用法

import com.omenkk.juc.util.Slepper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;
@Slf4j
public class demo2 {

    public static void main(String[] args) {
        ReentrantLock lock=new ReentrantLock();
        Thread t1=new Thread(()->{
            try{
                lock.lockInterruptibly();
            }catch (InterruptedException e){
                log.debug("被打断");
                return;
            }
            log.debug("获取了锁");
            log.debug("释放锁");
            lock.unlock();
        });
        log.debug("主线程获取锁");
        lock.lock();
        t1.start();
        try{
            Slepper.sleep(1);
            log.debug("执行打断");
            t1.interrupt();
        }finally {
            lock.unlock();
        }
    }
}
